<?php
namespace app\common\model;

use think\Model;

/**
 * 客户模型
 * Class AdminUser
 * @package app\common\model
 */
class Customer extends Model
{
    /**
     * 查询一条用户信息
     *
     * @param array $params
     * @param bool $fields
     * @param bool $need_array
     * @return array
     */
    public function getInfo(array $params, $fields = true, $need_array = false)
    {
        $where = $res = [];
        if (isset($params['id']) && $params['id'] > 0) {
            $where['id'] = $params['id'];
        }

        if (isset($params['company_name']) && !empty($params['company_name'])) {
            $where['company_name'] = $params['company_name'];
        }

        if (isset($params['taxpayer_identity_numbe']) && !empty($params['taxpayer_identity_numbe'])) {
            $where['taxpayer_identity_numbe'] = $params['taxpayer_identity_numbe'];
        }

        if (isset($params['customer_number']) && !empty($params['customer_number'])) {
            $where['customer_number'] = $params['customer_number'];
        }

        if (!empty($where)) {
            if (isset($params['is_delete']) && is_numeric($params['is_delete'])) {
                $where['is_delete'] = $params['is_delete'];
            } else {
                $where['is_delete'] = 0;
            }

            $res = $this->where($where)->field($fields)->find();
            if (!empty($res) && $need_array) {
                $res = $res->toArray();
            }
        }

        return $res;
    }

    /**
     * @param $id
     * @param array $req
     * @return bool
     */
    public function saveDate($id, array $req)
    {
        if ($id <= 0) {
            return false;
        }

        $this->data = [];
        $data = $req;
        $data['update_time'] = time();

        $res = $this->save($data, ['id' => $id]);
        // 清空原始数据，防止上一条对下一条的影响
        $this->origin = [];
        return $res;
    }

    /**
     * @param array $data
     * @return bool|mixed
     */
    public function addData(array $data)
    {
        $this->data = [];
        $res = $this->allowField(true)->data($data,true)->isUpdate(false)->save();
        $return = ($res && isset($this->id) && $this->id > 0) ? $this->id : false;
        // 清空原始数据，防止上一条对下一条的影响
        $this->origin = [];
        return $return;
    }

    /**
     * 查询列表
     *
     * @author mayy
     * @date 2017-8-15
     * @param array $params
     * @param $limit
     * @param bool $fields
     * @param bool $is_count
     * @param bool $is_need_arr
     * @return array
     */
    public function getList(array $params, $limit = 20, $is_count = false, $fields = true, $is_need_arr = false)
    {
        $where = $return = [];

        if (isset($params['id']) && !empty($params['id'])) {
            if (is_array($params['id'])) {
                $where['id'] = ['in', $params['id']];
            } else {
                $where['id'] = $params['id'];
            }
        }

        //关键字搜索：
        if (!empty($params['customer_name'])) {
            $where['company_name|short_name'] = ['like', '%' . $params['customer_name'] . '%'];
        }

        if (isset($params['company_name']) && !empty($params['company_name'])) {
            $where['company_name'] = ['like', '%' . $params['company_name'] . '%'];
        }

        if (isset($params['customer_number_arr']) && !empty($params['customer_number_arr'])) {
            $where['customer_number'] = ['in', $params['customer_number_arr']];
        }

        if (isset($params['customer_number']) && !empty($params['customer_number'])) {
            $where['customer_number'] = ['like', '%' . $params['customer_number'] . '%'];
        }

        if (isset($params['short_name']) && !empty($params['short_name'])) {
            $where['short_name'] = ['like', '%' . $params['short_name'] . '%'];
        }

        /*税号搜索*/
        if (isset($params['taxpayer_identity_numbe']) && !empty($params['taxpayer_identity_numbe'])) {
            $where['taxpayer_identity_numbe'] = ['like', '%' . $params['taxpayer_identity_numbe'] . '%'];
        }

        if (isset($params['province_id']) && $params['province_id'] > 0) {
            $where['province_id'] = $params['province_id'];
        }

        if (isset($params['city_id']) && $params['city_id'] > 0) {
            $where['city_id'] = $params['city_id'];
        }

        if (isset($params['district_id']) && $params['district_id'] > 0) {
            $where['district_id'] = $params['district_id'];
        }

        if (isset($params['star_level']) && $params['star_level'] > 0) {
            $where['star_level'] = $params['star_level'];
        }

        if (isset($params['is_general_taxpayer']) && is_numeric($params['is_general_taxpayer'])) {
            $where['is_general_taxpayer'] = $params['is_general_taxpayer'];
        }

        /*客户数据录入时间区间*/
        if (isset($params['start']) && !empty($params['end'])) {
            $where['create_time'] = ['between', [$params['start'], $params['end']]];
        }

        if (isset($params['is_delete']) && is_numeric($params['is_delete'])) {
            $where['is_delete'] = $params['is_delete'];
        } else {
            $where['is_delete'] = 0;
        }

        /*所属公司搜索*/
        if (isset($params['sign_company_code']) && !empty($params['sign_company_code'])) {
            if (is_array($params['sign_company_code'])) {
                $where['sign_company_code'] = ['in', $params['sign_company_code']];
            } else {
                $where['sign_company_code'] = $params['sign_company_code'];
            }
        }

        if (isset($where['order_str'])) {
            $order = $where['order_str'];
        } else {
            $order = 'id desc';
        }

        $count = $this->where($where)->count();
        if ($is_count) {
            return $count;
        }

        $data = $this->where($where)->field($fields)->order($order)->paginate($limit, false, ['query' => request()->param()]);

        return $is_need_arr ? $data->toArray() : $data;
    }

    /**
     * 获取最新的已客户序号
     */
    public function getLastCustomerNumber()
    {
        $res = $this->field('max(number) as number')->find();
        $res = $res->toArray();
        if (empty($res['number'])) {
            return 0;
        } else {
            return $res['number'];
        }
    }


    /**
     * 通过id获取名称
     * @param array $ids
     * @return array
     */
    public function getCustomerIdNameMap(array $ids)
    {
        if (empty($ids)) {
            return [];
        }

        $where = [
            'id' => ['in', $ids]
        ];
        $list = $this->where($where)->field('id,company_name,customer_number')->paginate(count($ids));
        $list = $list->toArray();
        $list = $list['data'];

        if (empty($list)) {
            return [];
        }

        $map_arr = [];
        foreach ($list as $v) {
            $map_arr[$v['id']] = $v;
        }

        return $map_arr;
    }


}